 {% load staticfiles %}
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="{% static '' %}default/css/bootstrap.min.css" rel="stylesheet">
      <link href="{% static '' %}default/css/style.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
{% include 'default/block_nav.html' %}


<div class="container theme-showcase" role="main">


      <div class="page-header">
        <h1>Products {% if word %} - {{ word.keyword }} <i class="glyphicon glyphicon-download-alt"><a target="_blank" href="{% url 'downloadcsv' %}?type=p&wd={{ word.id }}">下载数据</a></i>{% else %}<a href="#" onclick="history.go(-1)">返回</a>{% endif %}</h1>
      </div>
      <div class="row">
        <div class="col-md-12">
          <table class="table table-striped">
            <thead>
              <tr>
                <th>#</th>
                <th>关键词</th>
                <th>产品主图</th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '14' %}14{% else %}15{% endifnotequal %}">{% ifequal o '15' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}价格</a></th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '12' %}12{% else %}13{% endifnotequal %}">{% ifequal o '13' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}心愿单</a></th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '10' %}10{% else %}11{% endifnotequal %}">{% ifequal o '11' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}成交量</a></th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '8' %}8{% else %}9{% endifnotequal %}">{% ifequal o '9' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}投票</a></th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '6' %}6{% else %}7{% endifnotequal %}">{% ifequal o '7' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}评分</a></th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '4' %}4{% else %}5{% endifnotequal %}">{% ifequal o '5' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}评论</a></th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '2' %}2{% else %}3{% endifnotequal %}">{% ifequal o '3' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}近6月成交量</a></th>
                <th><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={% if pg %}{{ pg }}{% else %}1{% endif %}&o={% ifnotequal o '0' %}0{% else %}1{% endifnotequal %}">{% ifequal o '1' %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-top"></i>{% else %}<i style="font-size: 10px;" class="glyphicon glyphicon-triangle-bottom"></i>{% endifequal %}更新时间</a></th>
                <th>基础信息</th>
                <th>交易记录</th>
                <th>移除</th>
              </tr>
            </thead>
            <tbody>
            {% for product in productList %}
              <tr>
                <td>{% if request.user in product.user.all %}<i class="glyphicon glyphicon-star"></i> {% else %}<i class="glyphicon glyphicon-star-empty active" onclick="addfav({{ product.product_id }})"></i>{% endif %}</td>
                <td>{% for k in product.keyword.all %}<a href="{% url 'aliexpress:products' %}?word={{ k.id }}">{{ k.keyword }}</a>{% if not forloop.last %}<br>{% endif %}{% endfor %}</td>
                <td><a href="{% url 'aliexpress:product_info' product.product_id %}"><img data-src="{{ product.thumb }}" class="img-thumbnail" alt="{{ product.title }}" src="{{ product.thumb }}" data-holder-rendered="true" style="width: 60px; height: 60px;"></a></td>
                <td class="price-{{ product.product_id }}">{% ifnotequal product.low_price product.high_price%}{{ product.low_price }}-{{ product.high_price }}{% else %}{{ product.high_price }}{% endifnotequal %}</td>
                <td class="wish-{{ product.product_id }}">{{ product.wish_list }}</td>
                <td class="order-{{ product.product_id }} {% if not product.orders %}loading{% endif %}">{{ product.orders }}</td>
                <td class="votes-{{ product.product_id }}">{{ product.votes }}</td>
                <td class="rating-{{ product.product_id }} {% if not product.average_star_rating %}loading{% endif %}">{{ product.average_star_rating }}</td>
                <td class="feedback-{{ product.product_id }}">{{ product.all_feedback }}</td>
                <td class="sum-{{ product.product_id }} {% if not product.transactions_in_last_six_months %}loading{% endif %}">{{ product.transactions_in_last_six_months }}</td>
                <td class="time-{{ product.product_id }}">{{ product.update_date|date:"Y/m/d H:i:s" }}</td>
                <td class="sta2-{{ product.product_id }}">{% ifequal 0 product.status %}<i class="glyphicon glyphicon-play-circle" onclick="get_product('{{ product.product_id }}')"></i>{% else %}<i class="glyphicon glyphicon-ok-sign"></i>{% endifequal %}</td>
                <td class="sta-{{ product.product_id }}">{% ifequal 0 product.status_trans %}<i class="glyphicon glyphicon-play-circle" onclick="get_transactions('{{ product.product_id }}')"></i>{% else %}<i class="glyphicon glyphicon-ok-sign"></i>{% endifequal %}</td>
                <td><i class="glyphicon glyphicon-remove" onclick="removeproduct('{{ product.product_id }}')"></i></td>
              </tr>
            {% endfor %}
            </tbody>
          </table>
        </div>


      <div class="container text-center">
          <ul class="pagination">
              {% if productList.has_previous %}
            <li>
              <a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={{ productList.previous_page_number }}{% if o %}&o={{ o }}{% endif %}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
              </a>
            </li>
                  {% else %}
                  <li class="disabled">
              <a href="#" aria-label="Previous">
                <span aria-hidden="false">&laquo;</span>
              </a>
            </li>
              {% endif %}
          {% for p in productList.paginator.page_range %}
          {% ifequal p productList.number %}
            <li class="active"><a href="#">{{ p }}</a></li>
              {% else %}
              <li><a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={{ p }}{% if o %}&o={{ o }}{% endif %}">{{ p }}</a></li>
          {% endifequal %}
        {% endfor %}
          {% if productList.has_next %}
            <li>
              <a href="{% if wd %}?word={{ wd }}&{% else %}?{% endif %}page={{ productList.next_page_number }}{% if o %}&o={{ o }}{% endif %}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
              </a>
            </li>
          {% else %}
              <li class="disabled">
              <a href="#" aria-label="Next">
                <span aria-hidden="false">&raquo;</span>
              </a>
            </li>
          {% endif %}
          </ul>
      </div>

    </div>
</div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{% static '' %}default/js/bootstrap.min.js"></script>
  <script>
      $(".loading").html('<img src="{% static "images/loading_sm.gif" %}" width=50 />');
      function get_transactions(productId){
          alert("开始采集交易记录。。。");
          $.post("{% url 'aliexpress:signle_product_transactions' %}",{productId:productId},function(result){
              //$("span").html(result);
              if(result.error==1){
                  alert(result.msg);
                  return false;
              }else{
                  $(".sum-"+productId).html(result.transactions);
                  $(".time-"+productId).html(result.update_time);
                  $(".sta-"+productId).html('<i class="glyphicon glyphicon-ok-sign"></i>');
                  alert("采集完毕！");
              }
          });
      }

      function get_product(productId){
          alert("开始采集产品信息。。。");
          $.post("{% url 'aliexpress:signle_product_detail' %}",{productId:productId},function(result){
              //$("span").html(result);
              if(result.error==1){
                  alert(result.msg);
                  return false;
              }else{
                  if(result.lowPrice == result.highPrice){
                      $(".price-"+productId).html(result.lowPrice);
                  }else{
                      $(".price-"+productId).html(result.lowPrice+' - '+result.highPrice);
                  }

                  $(".wish-"+productId).html(result.wishList);
                  $(".order-"+productId).html(result.orders);
                  $(".votes-"+productId).html(result.votes);
                  $(".rating-"+productId).html(result.averageStarRating);
                  $(".feedback-"+productId).html(result.allFeedback);
                  $(".sta2-"+productId).html('<i class="glyphicon glyphicon-ok-sign"></i>');
                  alert("采集完毕。。。");
              }
          });
      }

      function removeproduct(id){
          location.href = "{% url 'aliexpress:remove_product' %}?id="+id
      }

      function addmonitor(id){
          location.href = "{% url 'aliexpress:monitor_add' %}?id="+id
      }

      function addfav(id){
          //location.href = "{% url 'aliexpress:monitor_add' %}?id="+id
          location.href = "{% url 'favorite_add' %}?id="+id
      }
  </script>
  {% include 'default/tongji.html' %}
  </body>
</html>